<template>
  <div class="login-wrap">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          :model="loginForm"
          ref="loginFormDom"
          :rules="rules"
          class="login-wrap_form"
        >
          <h1>您好！</h1>
          <h2>欢迎来到阿文的后台管理系统</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              v-model="loginForm.password"
              :show-password="true"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="login" class="login-btn" type="primary">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { useRouter, useRoute } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { greet } from '@/utils/time'
import user from '@/store/modules/user'
import { ElMessage } from 'element-plus'
const $route = useRoute()
const $router = useRouter()
let userStore = user()
let loginForm = reactive({
  username: '',
  password: '',
})
let loginFormDom = ref()
let rules: any = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '输入3-16位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 16, message: '输入3-16位', trigger: 'blur' },
  ],
})
function login() {
  console.log()

  loginFormDom.value.validate().then((res) => {
    console.log(res)
    if (res) {
      userStore.userLogin(loginForm).then(
        (res) => {
          console.log(res)

          ElMessage({
            message: '登录成功 ' + greet(),
            type: 'success',
          })
          console.log($route.query.redirect)

          $router.push({ path: ($route.query.redirect as string) || '/' })
        },
        (erroe) => {
          ElMessage({
            message: '登录失败，请检查账号密码',
            type: 'error',
          })
          console.log(erroe)
        },
      )
    }
  })
}
</script>

<style lang="scss" scoped>
.login-wrap {
  width: 100%;
  height: 100vh;
  // background-color: aqua;
  background: url(../../assets/images/background.jpg);
  background-size: cover;
  &_form {
    padding: 25px 16px;
    position: relative;
    top: 30vh;
    width: 70%;
    min-height: 150px;
    // background-color: aqua;
    background: url(../../assets/images/login_form.png);
    background-size: cover;
    h1 {
      color: pink;
      font-size: 40px;
      font-weight: 400;
    }
    h2 {
      margin: 16px 0;
      color: lightcoral;
      font-size: 20px;
      font-weight: 400;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
